<template>
  <div class="header subwrapper">
    <div class="letf-aside">
      <div class="rec-main" v-if="articleList.length > 0">
        <ul class="rec-title">
          <li><a href="#">推荐</a><span style="color: #f1f1f1">|</span></li>
          <li><a href="#">最新</a><span style="color: #f1f1f1">|</span></li>
          <li><a href="#">热榜</a></li>
        </ul>
        <div class="rec-contain">
          <ul>
            <li class="advertising">
              <a href="">
                <div class="ad-header">
                  <div class="UserInfo">
                    <el-popover placement="bottom" title="掘金酱" :width="200" trigger="hover" content="掘金酱 @x稀土掘金CEO">
                      <template #reference>
                        <el-button link>掘金酱</el-button>
                      </template>
                    </el-popover>
                    <span>4天前</span>
                  </div>
                  <el-button pian size="small">广告</el-button>
                </div>
                <div class="ad-main">
                  <div class="advertext">
                    <h3 class="ellipsis">掘金技术社区签约计划进行中│写作变现、流量扶持等你投稿!</h3>
                    <p class="ellipsis-2">投稿不仅能瓜分奖池，成为签约作者还可开启写作变现之路>></p>
                  </div>
                  <div class="adverpic">
                    <img src="@/assets/images/adver.png" alt="" />
                  </div>
                </div>
              </a>
            </li>
          </ul>

          <ul class="article-list">
            <li
              class="entry"
              v-for="(item, index) in articleList"
              :key="index"
              @click="router.push({ path: '/post', query: { params: encodeURI(JSON.stringify(item)) } })"
            >
              <div class="entry-header">
                <div class="UserInfo">
                  <el-popover
                    :width="270"
                    placement="top"
                    popper-style="box-shadow: rgb(14 18 22 / 35%) 0px 10px 38px -10px, rgb(14 18 22 / 20%) 0px 10px 20px -15px; padding: 20px;"
                  >
                    <template #reference>
                      <div>
                        <el-button link>{{ item.nick_name }} </el-button>
                      </div>
                    </template>
                    <template #default>
                      <div class="demo-rich-conent" style="display: flex; gap: 16px; flex-direction: column">
                        <div class="headerline" style="display: flex">
                          <el-avatar :size="60" src="https://avatars.githubusercontent.com/u/72015883?v=4" style="margin-bottom: 8px" />
                          <div style="flex: 1">
                            <div class="demo-rich-content__name" style="margin: 10px 0; font-weight: 500; display: flex">
                              <p style="margin-right: 5px">{{ item.nick_name }}</p>
                              <img src="https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/2c3fafd535a0625b44a5a57f9f536d77.svg" alt="" />
                            </div>
                            <p
                              class="demo-rich-content__mention ellipsis"
                              style="width: 170px; ,margin: 0; font-size: 14px; color: var(--el-color-info)"
                            >
                              <span v-for="i in item.tags" :key="i.tag_id">{{ i.tag_name }}</span>
                            </p>
                          </div>
                        </div>
                        <div class="demo-rich-content__desc" style="margin: 0; display: flex; justify-content: space-between">
                          <div style="display: flex; flex-direction: column; justify-content: center; align-items: center">
                            <span style="font-weight: 700; font-size: large">31</span><span style="color: var(--el-color-info)">关注</span>
                          </div>
                          <div style="display: flex; flex-direction: column; justify-content: center; align-items: center">
                            <span style="font-weight: 700; font-size: large">31</span><span style="color: var(--el-color-info)">关注者</span>
                          </div>
                          <el-button type="success" plain style="border-radius: 0; margin-top: 10px">关注</el-button>
                        </div>
                      </div>
                    </template>
                  </el-popover>
                  <span>{{ item.created_at }}</span>
                  <span v-for="(i, index2) in item.tags" :key="i.tag_id">{{ i.tag_name }} <i v-if="index2 !== item.tags.length - 1">·</i></span>
                </div>
                <span class="closebtn" large link @click="handleDelArticle(item.article_id)">
                  <el-icon><Close /></el-icon>
                </span>
              </div>
              <div class="entry-main">
                <div class="article-text">
                  <h3 class="ellipsis">
                    {{ item.title }}
                  </h3>
                  <p class="ellipsis">
                    {{ item.describe }}
                  </p>
                  <div class="moren-btn">
                    <span
                      ><i class="iconfont icon-view"></i><span>{{ item.pv > 0 ? item.pv : 0 }}</span></span
                    >
                    <span class="iconhouver" @click.prevent="handleUp(index, item.id)">
                      <i class="iconfont icon-icon" v-if="!item.upStatus"></i>
                      <i class="iconfont icon-dianzan_kuai" v-else style="color: #1e80ff"></i>
                      <span>{{ item.up > 0 ? item.up : '点赞' }}</span>
                    </span>
                    <span class="iconhouver"
                      ><i class="iconfont icon-pinglun"></i><span>{{ item.comment_num > 0 ? item.comment_num : '评论' }}</span></span
                    >
                  </div>
                </div>
                <div class="article-pic" v-if="item.img">
                  <img :src="item.img" alt="" />
                </div>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <!-- 首页骨架屏 -->
      <div class="skeleton" v-for="i in 6" :key="i" v-else>
        <span>
          <CxjSkeleton bg="#e1e1e1" width="50px" height="14px" animated></CxjSkeleton>
          <CxjSkeleton bg="#e1e1e1" width="50px" height="14px" animated style="margin: 0 10px"></CxjSkeleton>
          <CxjSkeleton bg="#e1e1e1" width="80px" height="14px" animated></CxjSkeleton>
        </span>
        <div class="skeleton-main">
          <span class="skeleton-left">
            <CxjSkeleton bg="#e1e1e1" width="400px" height="20px" animated></CxjSkeleton>
            <CxjSkeleton bg="#e1e1e1 " width="500px" height="18px" animated></CxjSkeleton>
            <CxjSkeleton bg="#e1e1e1" width="160px" height="18px" animated></CxjSkeleton>
          </span>
          <span>
            <CxjSkeleton bg="#e1e1e1" width="120px" height="80px" animated></CxjSkeleton>
          </span>
        </div>
      </div>
    </div>
    <div class="right-aside">
      <div class="signinbox">
        <right-aside></right-aside>
      </div>
    </div>
  </div>
</template>

<script setup>
import rightAside from '@/components/home/rightaside.vue'
import { reactive, ref, getCurrentInstance, onMounted, onUnmounted } from 'vue'
import { getHomeList, delArticle } from '@/api/article.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
const store = useStore()
const router = useRouter()
const { proxy } = getCurrentInstance()
const articleList = ref([])
const handleUp = (index, id) => {
  /* 实现点赞up加1 取消点赞减一*/
  if (articleList[index].upStatus) {
    articleList[index].upStatus = false
    articleList[index].up--
  } else {
    articleList[index].upStatus = true
    articleList[index].up++
  }
  console.log(articleList[index].upStatus)
  /* 发送点赞请求 */
}
const articleData = reactive({
  page: 1,
  size: 10,
  tag_id: 0,
  search: '',
  user_id: store.state.user.profile.user_id,
  concern: true, //为true时是我关注的用户发的帖子(这时需要传token,其他时候不要传)
  type: 1, //1帖子(默认)2沸点
  sort: 0 //0发布时间倒叙排序(默认)1按点击
})
const initArticleList = async () => {
  const { page, size, tag_id, search, user_id, type, sort } = articleData
  console.log(user_id)
  const res = await getHomeList({
    page,
    size,
    tag_id,
    search,
    type,
    sort
  })
  console.log(res, '文章列表')
  if (res.status !== 200) {
    return ElMessage({
      message: '获取数据失败,请稍后再试!',
      type: 'info'
    })
  }
  const { data } = res.data.data
  // console.log(data, '文章列表')
  /* 判断pic是否为数组 */
  if (Array.isArray(data)) {
    data.forEach((item) => {
      if (Array.isArray(item.pic)) {
        item.img = item.pic[0]
      } else {
        item.img = item.pic
      }
      /* 获取当前时间戳 */
      item.timeData = proxy.$moment(item.created_at * 1000).format('llll')
      item.created_at = proxy.$moment(item.created_at * 1000).fromNow()
    })
  }
  articleList.value = data
  articleList.value.forEach((item) => {
    item.upStatus = false
  })
  console.debug(articleList.value, 'articleList')
}
initArticleList()
const handleDelArticle = (id) => {
  console.log(id, 'id')
  ElMessageBox.confirm('确定要删除该条文章吗?', 'Warning', {
    confirmButtonText: 'OK',
    cancelButtonText: 'Cancel',
    type: 'warning',
    center: true
  })
    .then(async () => {
      const token = localStorage.getItem('token')

      await delArticle({ Article_id: id, token })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消删除'
      })
    })
}
/* 无限刷新 */
let scrollStatus = false
function InfiniteScroll() {
  let scrollTop = document.documentElement.scrollTop
  let clientHeight = document.documentElement.clientHeight
  let scrollHeight = document.documentElement.scrollHeight
  if (scrollTop + clientHeight > scrollHeight - 350) {
    if (!scrollStatus) {
      scrollStatus = true
      setTimeout(() => {
        articleData.size += 5
        initArticleList()
        scrollStatus = false
      }, 800) //设置节流时间
    }
  }
}
//监听滚动事件
onMounted(() => {
  window.addEventListener('scroll', InfiniteScroll)
})
//移除滚动事件
onUnmounted(() => {
  window.removeEventListener('scroll', InfiniteScroll)
})
defineExpose({
  initArticleList
})
</script>

<style lang="scss" scoped>
.skeleton {
  margin-top: 30px;
  height: 90px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .skeleton-main {
    display: flex;
    justify-content: space-between;
    .skeleton-left {
      height: 80px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .cxj-skeleton {
        margin-top: 10px;
      }
    }
  }
}
.cxj-skeleton {
  animation: fade 1s linear infinite alternate;
}
@keyframes fade {
  from {
    opacity: 0.2;
  }
  to {
    opacity: 1;
  }
}
.header {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
}
.letf-aside {
  width: 700px;
  .rec-main {
    background-color: #fff;
    .rec-title {
      border-bottom: 1px solid #f4f4f4;
      display: flex;
      align-items: center;
      height: 50px;
      padding: 10px 20px;
      li {
        a {
          color: #333;
          font-size: 14px;
          display: inline-block;
          padding: 15px;
          &:hover {
            color: $jjColor;
          }
          &.active {
            color: $jjColor;
          }
        }
      }
    }
  }
}
.right-aside {
  width: 240px;
}
.rec-contain {
  padding: 12px 15px;
  .advertising {
    border-bottom: 1px solid #f4f4f4;
    padding-bottom: 12px;
    .ad-header {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      .UserInfo {
        display: flex;
        flex-direction: row;
        align-items: center;
        .el-popover {
          margin-right: 10px;
        }
        span {
          margin-left: 10px;
          font-size: 12px;
        }
      }
    }
  }
}
.ad-main {
  padding-left: 3px;
  margin-top: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .advertext {
    width: 500px;
    h3 {
      margin-bottom: 20px;
    }
  }
}
.adverpic {
  width: 120px;
  height: 80px;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
/* 文章列表 */
.article-list {
  .entry {
    width: 100%;
    padding: 16px 0;
    border-bottom: 1px solid #f4f4f4;
    .closebtn {
      opacity: 0;
      transition: all 0.3s ease-in-out;
    }
    &:hover .closebtn {
      opacity: 1;
    }
    .entry-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .UserInfo {
        display: flex;
        flex-direction: row;
        align-items: center;
        .el-popover {
          margin-right: 10px;
        }
        span {
          margin-left: 10px;
          font-size: 12px;
        }
      }
    }
    .entry-main {
      display: flex;
      margin-top: 8px;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      cursor: pointer;

      .article-text {
        width: 500px;
        height: 97px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .moren-btn {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          .iconhouver {
            &:hover {
              color: $jjColor;
            }
          }
          span {
            margin-right: 10px;
            font-size: 12px;
          }
          .iconfont {
            display: inline-block;
            vertical-align: -5%;
            margin-right: 5px;
          }
        }
      }
      .article-pic {
        width: 120px;
        height: 80px;
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }
  }
}
</style>
